#include "callbacks.h"
#include "mem_structures.h"

#include <assert.h>
void destroy_real(Ewl_Widget *w, void *event, void *data)
{
    ewl_widget_destroy(w);
    ewl_main_quit();
}

void destroy_cb_real(Ewl_Widget *w, void *event, void *data)
{
    run_dialog(w,event,data);
}

void text_update_cb(Ewl_Widget *w, void *event, void *data)
{
    char *s = NULL;
    Ewl_Widget *label = NULL;
    char buf[BUFSIZ];

    s = ewl_text_text_get(EWL_TEXT(w));
    label = (Ewl_Widget *)data;

    snprintf(buf, BUFSIZ, "Hello %s", s);
    ewl_text_text_append(EWL_TEXT(label), buf);

    free(s);
}
void dialog_delete_cb(Ewl_Widget *w, void *ev, void *data)
{
    ewl_widget_destroy(EWL_WIDGET(data));
}
void dialog_response_cb(Ewl_Widget *w, void *ev, void *data)
{
    Ewl_Stock_Type response;
    Ewl_Widget *o;
    Ewl_Window *win;

    o = ewl_widget_name_find("Quit Dialog");
    response = ewl_stock_type_get(EWL_STOCK( w));

    if (response == EWL_STOCK_OK)
    {
        ewl_widget_destroy(EWL_WIDGET(data));
        win = EWL_WINDOW(ewl_embed_widget_find(w));
        destroy_real(w,ev,data);
    }
    else
    {
        ewl_widget_destroy(EWL_WIDGET(data));
    }

}

void eedit_key_down_cb(Ewl_Widget *w, void *ev, void *data)
{
    buffer_t *buffer;
    Ewl_Event_Key *event;
    Ewl_Entry *e;
    int position;

    buffer = gui_get_buffer_for_widget(EWL_WIDGET(w));

    assert(buffer!=NULL);

    event = ev;
    e = EWL_ENTRY(w);
    position = ewl_entry_cursor_position_get(EWL_ENTRY_CURSOR(e->cursor));


    /* Should we reset the cursor blink thinger? */
#if 0

    ewl_widget_state_set(EWL_WIDGET(e->cursor), "noblink",
                         EWL_STATE_PERSISTENT);
#endif


    if (!event->keyname)
        return;

    if (!strcmp(event->keyname, "Left"))
    {
        ewl_entry_cursor_move_left(e);
        return;
    }

    if (!strcmp(event->keyname, "Right"))
    {
        ewl_entry_cursor_move_right(e);
        return;
    }

    if (!strcmp(event->keyname, "Up"))
    {
        ewl_entry_cursor_move_up(e);
        return;
    }

    if (!strcmp(event->keyname, "Down"))
    {
        ewl_entry_cursor_move_down(e);
        return;
    }

    if (!strcmp(event->keyname, "BackSpace"))
    {
        /* Do eedit stuff */
        return;
    }

    if (!strcmp(event->keyname, "Delete"))
    {
        /* Do eedit stuff */
        return;
    }

    if ((!strcmp(event->keyname, "Return"))
            || (!strcmp(event->keyname, "KP_Return"))
            || (!strcmp(event->keyname, "Enter"))
            || (!strcmp(event->keyname, "KP_Enter"))
            || (!strcmp(event->keyname, "\n")))
    {
        mem_addChar(buffer,'\n',position);
        gui_update_text(buffer);
        ewl_text_cursor_position_set(EWL_TEXT(e),position+1);
        return;
    }

    if (!(event->modifiers & EWL_KEY_MODIFIER_CTRL))
    {
        printf("Key[%s] at position [%i]\n",event->keyname,position);
        mem_addChar(buffer,event->keyname[0], position);
        gui_update_text(buffer);
        ewl_text_cursor_position_set(EWL_TEXT(e),position+1);
    }

    return;
}

void run_dialog(Ewl_Widget *w, void *ev, void *data)
{
    Ewl_Widget *o, *dialog, *hbox;
    Ewl_Widget *win = EWL_WINDOW(ewl_embed_widget_find(w));

    dialog = ewl_dialog_new();
    ewl_window_title_set(EWL_WINDOW(dialog), "Quit Dialog");
    ewl_window_name_set(EWL_WINDOW(dialog), "QuitDialog");
    ewl_window_class_set(EWL_WINDOW(dialog), "EFL Test Application");
    ewl_window_transient_for(EWL_WINDOW(dialog),EWL_WINDOW(win));
    ewl_callback_append(dialog, EWL_CALLBACK_DELETE_WINDOW, dialog_delete_cb, NULL);
    ewl_widget_show(dialog);
    ewl_dialog_active_area_set(EWL_DIALOG(dialog), EWL_POSITION_TOP);

    hbox = ewl_hbox_new();
    ewl_container_child_append(EWL_CONTAINER(dialog), hbox);
    ewl_widget_show(hbox);

    o = ewl_image_new();
    ewl_image_file_set(EWL_IMAGE(o),
                       ewl_icon_theme_icon_path_get(EWL_ICON_DIALOG_INFORMATION, 46),
                       EWL_ICON_DIALOG_INFORMATION);
    ewl_container_child_append(EWL_CONTAINER(hbox), o);
    ewl_object_padding_set(EWL_OBJECT(o), 20,20,20,20);
    ewl_widget_show(o);

    o = ewl_text_new();
    ewl_object_fill_policy_set(EWL_OBJECT(o), EWL_FLAG_FILL_NONE);
    ewl_object_alignment_set(EWL_OBJECT(o), EWL_FLAG_ALIGN_CENTER);
    ewl_container_child_append(EWL_CONTAINER(hbox), o);
    ewl_text_text_set(EWL_TEXT(o), "Are you sure you want to exit?");
    ewl_widget_show(o);

    ewl_dialog_active_area_set(EWL_DIALOG(dialog), EWL_POSITION_BOTTOM);

    o = ewl_button_new();
    ewl_stock_type_set(EWL_STOCK(o), EWL_STOCK_OK);
    ewl_container_child_append(EWL_CONTAINER(dialog), o);
    ewl_callback_append(o, EWL_CALLBACK_CLICKED, dialog_response_cb, dialog);
    ewl_widget_show(o);

    o = ewl_button_new();
    ewl_stock_type_set(EWL_STOCK(o), EWL_STOCK_CANCEL);
    ewl_container_child_append(EWL_CONTAINER(dialog), o);
    ewl_callback_append(o, EWL_CALLBACK_CLICKED, dialog_response_cb, dialog);
    ewl_widget_show(o);

    ewl_dialog_active_area_set(EWL_DIALOG(dialog), EWL_POSITION_TOP);
    ewl_widget_show(dialog);
}
